package org.abc.program.controller.admin;

import com.github.pagehelper.util.StringUtil;
import org.abc.program.entity.Menu;
import org.abc.program.page.admin.Page;
import org.abc.program.service.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/admin/menu")
public class MenuController {

	@Resource
	private MenuService menuService;

	/***
	 * @param page 辅助分页
	 * @param name 这里的name变量对应list.jsp的搜索按钮触发的name变量
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST) // 返回list页面的POST请求
	@ResponseBody  // 把map转换成json格式报文
	public HashMap<String, Object> getMenuList(Page page, String name) {

		HashMap<String, Object> queryMap = new HashMap<>();
		queryMap.put("name", name);
		queryMap.put("offset", page.getOffset());  // limit的第一个参数
		queryMap.put("rows", page.getRows());  // limit的第二个参数
		List<Menu> menuList = menuService.findList(queryMap);

		HashMap<String, Object> map = new HashMap<>();
		map.put("rows", menuList);  // 结果集
		int total = menuService.getTotalCount(queryMap);
		map.put("total", total);  // 总记录数

		return map;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)  // 返回index页面对list菜单页面的GET请求
	public String toMenuList(Model model) {

		List<Menu> topMenu = menuService.findTopMenu();
		model.addAttribute("topList", topMenu);  // topList对应前端"顶级菜单"的键
		return "/menu/list";  // 对应"views/"下的逻辑视图名
	}

	/***
	 * 添加菜单逻辑
	 * @param menu
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody  // 把map转换成json格式报文
	public Map<String, String> addMenu(Menu menu) {

		Map<String, String> map = new HashMap<>();

		if (menu == null) {
			map.put("type", "error");
			map.put("msg", "请输入内容！");
			return map;
		}
		if (StringUtil.isEmpty(menu.getName())) {
			map.put("type", "error");
			map.put("msg", "菜单名不能为空！");
			return map;
		}
		if (StringUtil.isEmpty(menu.getUrl())) {
			map.put("type", "error");
			map.put("msg", "菜单URL不能为空！");
			return map;
		}
		if (StringUtil.isEmpty(menu.getIcon())) {
			map.put("type", "error");
			map.put("msg", "菜单图标不能为空！");
			return map;
		}

		int iret = menuService.addMenu(menu);
		if (iret == 0) {
			map.put("type", "error");
			map.put("msg", "菜单添加失败！");
			return map;
		}
		map.put("type", "success");
		map.put("msg", "菜单添加成功！");
		return map;
	}

	/***
	 * 加载菜单图标的方法
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "get_icons", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getIcons(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		String rootPath = request.getServletContext().getRealPath("/");  // 得到项目的根目录
		File file = new File(rootPath + "resources/admin/easyui/css/icons");
		List<String> icons = new ArrayList<>();
		if (file.exists() == false) {
			map.put("type", "error");
			map.put("msg", "文件目录不存在");
			return map;
		}

		File[] files = file.listFiles();
		for (File f : files) {
			if (f != null && f.getName().contains("png")) {
				String newIcon = f.getName().substring(0, f.getName().lastIndexOf(".")).replace("_", "-");
				String newName = "icon-" + newIcon;
				icons.add(newName);
			}
		}

		map.put("type", "success");
		map.put("content", icons);
		return map;
	}

	/**
	 * 更新菜单方法
	 *
	 * @param menu
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> editMenu(Menu menu) {

		Map<String, Object> map = new HashMap<>();
		if (menu == null) {
			map.put("type", "error");
			map.put("msg", "请选择正确的菜单信息!");
			return map;
		}
		if (StringUtil.isEmpty(menu.getName())) {
			map.put("type", "error");
			map.put("msg", "请填写菜单名称!");
			return map;
		}
		if (StringUtil.isEmpty(menu.getIcon())) {
			map.put("type", "error");
			map.put("msg", "请填写菜单图标类!");
			return map;
		}
		if (menu.getParentId() == null) {
			menu.setParentId(0L);
		}
		if (menuService.edit(menu) < 0) {
			map.put("type", "error");
			map.put("msg", "修改失败,请联系管理员!");
			return map;
		}

		map.put("type", "success");
		map.put("msg", "修改成功!");
		return map;
	}

	/**
	 * 删除菜单方法
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> deleteMenu(@RequestParam(name = "id") Integer id) {

		Map<String, String> map = new HashMap<String, String>();
		if (id == null) {
			map.put("type", "error");
			map.put("msg", "请选择要删除的菜单信息!");
			return map;
		}

		List<Menu> childerList = menuService.findChilderList(id);
		if (childerList != null && childerList.size() > 0) {
			//表示该分类下存在子分类，不能删除
			map.put("type", "error");
			map.put("msg", "该分类下存在子分类，不能删除!");
			return map;
		}
		if (menuService.delete(id) <= 0) {
			map.put("type", "error");
			map.put("msg", "删除失败,请联系管理员!");
			return map;
		}

		map.put("type", "success");
		map.put("msg", "删除成功!");
		return map;
	}
}
